package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.core.builder.FunctionAuthBuilder;
import com.oocl.euc.core.builder.FunctionBuilder;
import com.oocl.euc.core.builder.RoleBuilder;
import com.oocl.euc.core.builder.StatusBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Function;
import com.oocl.euc.core.maintenance.model.FunctionAuth;
import com.oocl.euc.core.maintenance.model.Role;
import com.oocl.euc.core.maintenance.model.Status;
import com.oocl.euc.core.maintenance.repository.FunctionAuthRepository;
import com.oocl.euc.core.maintenance.repository.FunctionRepository;
import com.oocl.euc.core.maintenance.repository.RoleRepository;
import com.oocl.euc.core.maintenance.repository.StatusRepository;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


public class FunctionControllerIT extends ITTestBase {

    @Autowired
    private FunctionBuilder functionBuilder;

    @Autowired
    private FunctionAuthBuilder functionAuthBuilder;

    @Autowired
    private StatusBuilder statusBuilder;

    @Autowired
    private RoleBuilder roleBuilder;

    @Autowired
    private FunctionAuthRepository functionAuthRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private StatusRepository statusRepository;

    @Autowired
    private FunctionRepository functionRepository;

    @After
    public void reset() {
        functionAuthRepository.deleteAll();
        roleRepository.deleteAll();
        statusRepository.deleteAll();
        functionRepository.deleteAll();
    }

    @Test
    public void should_return_function_auth_list_when_given_function_id() throws Exception {
        Status status1 = statusBuilder.defaultOverallStatusBuilder().saveToDB();
        Status status2 = statusBuilder.defaultOverallStatusBuilder().saveToDB();
        Role role1 = roleBuilder.defaultBuilder().saveToDB();
        Role role2 = roleBuilder.defaultBuilder().saveToDB();
        FunctionAuth f1 = functionAuthBuilder.defaultBuilder().withStatus(status1).withRole(role1).create();
        FunctionAuth f2 = functionAuthBuilder.defaultBuilder().withStatus(status2).withRole(role2).create();
        Set<FunctionAuth> functionAuthSet = new HashSet<>();
        functionAuthSet.addAll(Arrays.asList(f1, f2));
        Function function = functionBuilder.defaultBuilder().withFunctionAuth(functionAuthSet).saveToDB();
        Long functionId = function.getId();
        mockMvc.perform(get("/rest/function/findFunctionListAuth")
                .param("functionId", String.valueOf(functionId)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$").isNotEmpty())
                .andExpect(jsonPath("$.length()", is(2)))
                .andExpect(jsonPath("$[0].roleName", is(role1.getRoleName())))
                .andExpect(jsonPath("$[0].statusName", is(status1.getStatusName())));
    }

    @Test
    public void should_return_function_list_when_given_pcrs_number() throws Exception {
        mockMvc.perform(get("/rest/function/getFunctionList")
                .param("pcrsNum", "11"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$").isEmpty());
    }
}